package com.yiren.dbaa.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.yiren.dbaa.R;

/**
 * Created by lwb
 * on 2021/11/29
 * Describe 通用dialog
 */
public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    //提示信息对话框
    public CustomDialog(Context context, String title, String content) {
        super(context);
        Builder builder = new Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setCanceled(false);
        builder.setUpApk(true);
        builder.setPositiveButton("我知道了", (dialog, which) -> dialog.dismiss());
        builder.setNegativeButton(null, (dialog, which) -> dialog.dismiss());
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private int size;
        private boolean isCanceled = true;
        private boolean isUpApk = false;  //如果是更新说明则内容可滑动，并且内容居左显示
        private String positiveButtonText;
        private String negativeButtonText;
        private String centretiveButtonText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private OnClickListener contentButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param size
         * @return
         */
        public Builder setSize(int size) {
            this.size = size;
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the Dialog title from String
         * 是否点击空白处消失
         *
         * @return
         */

        public Builder setCanceled(boolean isCanceled) {
            this.isCanceled = isCanceled;
            return this;
        }

        public Builder setUpApk(boolean isUpApk) {
            this.isUpApk = isUpApk;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         * 左边按钮
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * 中间按钮
         *
         * @param centretiveButtonText
         * @param listener
         * @return
         */
        public Builder setCentretiveButton(int centretiveButtonText, OnClickListener listener) {
            this.centretiveButtonText = (String) context.getText(centretiveButtonText);
            this.contentButtonClickListener = listener;
            return this;
        }

        public Builder setCentretiveButton(String centretiveButtonText, OnClickListener listener) {
            this.centretiveButtonText = centretiveButtonText;
            this.contentButtonClickListener = listener;
            return this;
        }

        /**
         * 右边按钮
         *
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_custom, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            DisplayMetrics metric = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
            params.width = (metric.widthPixels * 6) / 10;
            params.height = (metric.heightPixels * 8) /10;
            dialog.setCanceledOnTouchOutside(isCanceled);// 设置点击屏幕Dialog不消失
            dialog.getWindow().setAttributes(params);
            // set the dialog title
            ((TextView) layout.findViewById(R.id.txt_dialog_title)).setText(title);
            dialog.setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (isCanceled) {
                        dialog.dismiss();
                        return false;
                    } else {
                        return true;
                    }
                }
            });

            // set the confirm button  
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.but_dialog_ok)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.but_dialog_ok)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);

                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.but_dialog_ok).setVisibility(View.GONE);
            }
            if (centretiveButtonText != null) {
                ((Button) layout.findViewById(R.id.but_dialog_centre)).setText(centretiveButtonText);
                if (contentButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.but_dialog_centre)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            contentButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.but_dialog_centre).setVisibility(View.GONE);
                layout.findViewById(R.id.verticel_view).setVisibility(View.GONE);
            }
            // set the cancel button  
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.but_dialog_clear))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.but_dialog_clear)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.but_dialog_clear).setVisibility(View.GONE);
            }
            // set the content message  
            if (message != null) {
                ((TextView) layout.findViewById(R.id.txt_dialog_message)).setText(message);
                if (isUpApk) {
                    ((TextView) layout.findViewById(R.id.txt_dialog_message)).setGravity(Gravity.LEFT);
                }
            } else {
                ((TextView) layout.findViewById(R.id.txt_dialog_message)).setText("系统出问题了，请提交反馈！");
//            	((TextView) layout.findViewById(R.id.txt_dialog_message)).setVisibility(View.GONE);
//    			((EditText) layout.findViewById(R.id.edt_dialog_message)).setVisibility(View.VISIBLE);
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}  